﻿using PivalSoft.LDMS.Data;
using PivalSoft.LDMS.Properties;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PivalSoft.LDMS
{
    public partial class frmCaseHearing : Form
    {
        public int UserId { get; set; }
        public int caseId = 0;
        private int caseHearingId = 0;

        public frmCaseHearing()
        {
            InitializeComponent();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (ValidateCaseHearing())
            {
                LDMSRepository repository = new LDMSRepository();
                CaseHearing hearing = new CaseHearing();
                hearing.AdvocateID = Convert.ToInt32(cmbPartyAdvocate.SelectedValue);
                hearing.CaseID = Convert.ToInt32(cmbcase.SelectedValue);
                hearing.CourtID = Convert.ToInt32(cmbCourt.SelectedValue);
                ////hearing.CreatedBy = UserId;
                ////hearing.CreatedDate = DateTime.Now;
                hearing.HearingDate = dtNextHearingdate.Value;
                hearing.JudgeName = txtJudgeName.Text.Trim();
                hearing.Notes = txtNotes.Text.Trim();
                hearing.OppositionPartyID = Convert.ToInt32(cmbOppositionParty.SelectedValue);
                hearing.StatusID = Convert.ToInt32(cmbStatus.SelectedValue);
                if (caseHearingId > 0)
                    hearing.CaseHearingID = caseHearingId;
                else
                {
                    hearing.CreatedBy = UserId;
                    hearing.CreatedDate = DateTime.Now;
                }
                repository.AddCaseHearing(hearing);
                MessageBox.Show(Resources.SaveCaseHearing, Resources.MessageCaption);
                ResetControl();
            }
        }

        private void ResetControl()
        {
            caseHearingId = 0;
            txtJudgeName.Text = string.Empty;
            dtNextHearingdate.Value = DateTime.Now;
            cmbPartyAdvocate.SelectedIndex = 0;
            cmbcase.SelectedIndex = 0;
            cmbCourt.SelectedIndex = 0;
            cmbOppositionParty.SelectedIndex = 0;
            cmbStatus.SelectedIndex = 0;
            txtNotes.Text = string.Empty;
        }

        private bool ValidateCaseHearing()
        {
            bool isValid = true;
            if (string.IsNullOrEmpty(txtJudgeName.Text.Trim()))
            {
                MessageBox.Show(Resources.RequireJudgeName, Resources.MessageCaption);
                isValid = false;
            }
            if (string.IsNullOrEmpty(txtNotes.Text.Trim()))
            {
                MessageBox.Show(Resources.RequireNotes, Resources.MessageCaption);
                isValid = false;
            }
            return isValid;
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void frmCaseHearing_Load(object sender, EventArgs e)
        {
            LDMSRepository repository = new LDMSRepository();
            var cases = repository.GetAllCases();
            cases.Insert(0, new Case { CaseID = 0, CaseTitle = "Select Case" });

            cmbcase.DataSource = cases;
            cmbcase.ValueMember = "CaseID";
            cmbcase.DisplayMember = "CaseTitle";
            cmbcase.SelectedValue = caseId;

            cmbCaseNo.DataSource = cases;
            cmbCaseNo.ValueMember = "CaseID";
            cmbCaseNo.DisplayMember = "CaseTitle";

            cmbPartyAdvocate.DataSource = repository.GetContactByType(1);
            cmbOppositionParty.DataSource = repository.GetContactByType(1);
            cmbStatus.DataSource = repository.GetTypes("CaseStatus");
            cmbCourt.DataSource = repository.GetAllCourts(0);
            CaseHearing hearing = repository.GetCaseHearByIding(caseId);
            if (hearing != null && hearing.CaseHearingID > 0)
            {
                cmbPartyAdvocate.SelectedValue = hearing.AdvocateID;
                cmbOppositionParty.SelectedValue = hearing.OppositionPartyID;
                cmbStatus.SelectedValue = hearing.StatusID;
                cmbCourt.SelectedValue = hearing.CourtID;
            }
            else
            {
                Case caseDetails = repository.GetCaseByCaseID(caseId);
                cmbPartyAdvocate.SelectedValue = caseDetails.AdvocateID;
                cmbOppositionParty.SelectedValue = caseDetails.OppositionPartyID;
                cmbStatus.SelectedValue = caseDetails.StatusID;
                cmbCourt.SelectedValue = caseDetails.CourtID;
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            SearchCaseHearing();
        }

        private void SearchCaseHearing()
        {
            List<KeyValuePair<string, string>> parameters = new List<KeyValuePair<string, string>>();
            if (cmbCaseNo.SelectedIndex > 0)
            {
                parameters.Add(new KeyValuePair<string, string>(Constants.CASE, Convert.ToString(cmbCaseNo.SelectedValue)));
            }
            if (!string.IsNullOrEmpty(txtClientName.Text.Trim()))
            {
                parameters.Add(new KeyValuePair<string, string>(Constants.PARTY, txtClientName.Text));
            }
            if (dtpCaseDate.Checked)
            {
                parameters.Add(new KeyValuePair<string, string>(Constants.HEARINGDATE, Convert.ToString(dtpCaseDate.Value)));
            }
            if (dtpNextHearing.Checked)
            {
                parameters.Add(new KeyValuePair<string, string>(Constants.NEXTHEARING, Convert.ToString(dtpNextHearing.Value)));
            }
            if (!string.IsNullOrEmpty(txtOppositeParty.Text.Trim()))
            {
                parameters.Add(new KeyValuePair<string, string>(Constants.OPPPARTY, txtOppositeParty.Text));
            }

            if (parameters.Count > 0)
            {
                LDMSRepository repository = new LDMSRepository();
                var contacts = repository.SearchHearing(parameters);
                dgCaseHearing.DataSource = contacts;
            }
            else
            {
                MessageBox.Show(Resources.NoneFilter, Resources.MessageCaption);
            }
        }

        private void dgCaseHearing_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (Convert.ToString(dgCaseHearing.Rows[e.RowIndex].Cells[e.ColumnIndex].Value).Equals(Constants.COL_EDIT))
            {
                txtJudgeName.Text = Convert.ToString(dgCaseHearing.Rows[e.RowIndex].Cells[Constants.COL_JUDGENAME].Value);

                dtNextHearingdate.Value = Convert.ToDateTime(dgCaseHearing.Rows[e.RowIndex].Cells[Constants.COL_LASTNAME].Value);

                cmbPartyAdvocate.SelectedIndex = cmbPartyAdvocate.Items.IndexOf(cmbPartyAdvocate.Items.Cast<TypeMaster>().FirstOrDefault(s =>
                    s.TypeMasterID == Convert.ToInt32(dgCaseHearing.Rows[e.RowIndex].Cells[Constants.COL_ADVOCATEID].Value)));

                cmbcase.SelectedIndex = cmbcase.Items.IndexOf(cmbcase.Items.Cast<TypeMaster>().FirstOrDefault(s =>
                    s.TypeMasterID == Convert.ToInt32(dgCaseHearing.Rows[e.RowIndex].Cells[Constants.COL_COLCASEID].Value)));

                cmbCourt.SelectedIndex = cmbCourt.Items.IndexOf(cmbCourt.Items.Cast<TypeMaster>().FirstOrDefault(s =>
                    s.TypeMasterID == Convert.ToInt32(dgCaseHearing.Rows[e.RowIndex].Cells[Constants.COL_COURTID].Value)));

                cmbOppositionParty.SelectedIndex = cmbOppositionParty.Items.IndexOf(cmbOppositionParty.Items.Cast<TypeMaster>().FirstOrDefault(s =>
                   s.TypeMasterID == Convert.ToInt32(dgCaseHearing.Rows[e.RowIndex].Cells[Constants.COL_OPPOSITIONPARTYID].Value)));

                cmbStatus.SelectedIndex = cmbStatus.Items.IndexOf(cmbStatus.Items.Cast<TypeMaster>().FirstOrDefault(s =>
                     s.TypeMasterID == Convert.ToInt32(dgCaseHearing.Rows[e.RowIndex].Cells[Constants.COL_STATUSID].Value)));

                txtNotes.Text = Convert.ToString(dgCaseHearing.Rows[e.RowIndex].Cells[Constants.COL_NOTES].Value);

                caseHearingId = Convert.ToInt32(dgCaseHearing.Rows[e.RowIndex].Cells[Constants.COL_COLCASEHEARINGID].Value);

                dgCaseHearing.DataSource = null;
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
